package org.xqh.study.juc.akka;

import akka.actor.UntypedActor;
import scala.Option;

/**
 * Created by leo on 2017/10/18.
 */
public class RestartActor extends UntypedActor {

    public static enum Msg{
        DONE,RESTART
    }

    @Override
    public void preStart() throws Exception {
        System.out.println("preStart hashCode:"+ this.hashCode());
    }

    @Override
    public void postStop() throws Exception {
        System.out.println("postStop hashCode:"+this.hashCode());
    }

    @Override
    public void postRestart(Throwable reason) throws Exception {
        super.postRestart(reason);
        System.out.println("postRestart hashCode:"+this.hashCode());
    }

    @Override
    public void preRestart(Throwable reason, Option<Object> message) throws Exception {
        System.out.println("preRestart hashCode:"+this.hashCode());
    }

    @Override
    public void onReceive(Object msg){
       if(msg == Msg.DONE){
           getContext().stop(getSelf());
       }else if(msg == Msg.RESTART){
           System.out.println(((Object)null).toString());
           //抛出异常 默认会被restart, 但这里会resume
           double a= 0/0;
       }else {
           unhandled(msg);
       }
    }
}
